<template>
  <div class="my-results">
    <div class="results-header">
      <h2>我的成绩</h2>
    </div>

    <el-card>
      <el-table :data="results" v-loading="loading" stripe>
        <el-table-column prop="exam" label="考试" width="200" />
        <el-table-column prop="score" label="分数" width="100">
          <template #default="{ row }">
            <span :class="getScoreClass(row.score)">{{ row.score }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="totalScore" label="满分" width="80" />
        <el-table-column label="得分率" width="100">
          <template #default="{ row }">
            {{ Math.round((row.score / row.totalScore) * 100) }}%
          </template>
        </el-table-column>
        <el-table-column prop="rank" label="排名" width="80" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 'published' ? 'success' : 'warning'">
              {{ row.status === 'published' ? '已发布' : '待发布' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="submitTime" label="提交时间" width="180" />
        <el-table-column label="操作" width="100">
          <template #default="{ row }">
            <el-button size="small" @click="viewDetail(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { gradingApi } from '@/api'

const loading = ref(false)
const results = ref([])

const fetchResults = async () => {
  loading.value = true
  try {
    // TODO: 实现获取个人成绩的API
    const response = await gradingApi.getStudentGrades(1) // 假设用户ID为1
    results.value = response.data?.results || []
  } catch (error) {
    console.error('获取成绩失败:', error)
    ElMessage.error('获取成绩失败')
  } finally {
    loading.value = false
  }
}

const getScoreClass = (score: number) => {
  if (score >= 90) return 'excellent'
  if (score >= 80) return 'good'
  if (score >= 60) return 'pass'
  return 'fail'
}

const viewDetail = (result: any) => {
  ElMessage.info('查看成绩详情功能开发中')
}

onMounted(() => {
  fetchResults()
})
</script>

<style scoped lang="scss">
.my-results {
  padding: 20px;

  .results-header {
    margin-bottom: 20px;

    h2 {
      margin: 0;
      color: #303133;
    }
  }

  .excellent { color: #67c23a; font-weight: bold; }
  .good { color: #409eff; }
  .pass { color: #e6a23c; }
  .fail { color: #f56c6c; }
}
</style>